package org.dromara.bykt.controller;

import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.bykt.domain.bo.BYKTSimulationInfoBo;
import org.dromara.bykt.domain.vo.BYKTSimulationInfoVo;
import org.dromara.bykt.service.IBYKTSimulationInfoService;
import org.dromara.bykt.utils.AliOSSUpload;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 试卷信息
 *
 * @author Hou
 * @date 2025-06-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bykt/info")
public class BYKTSimulationInfoController extends BaseController {

    private final IBYKTSimulationInfoService simulationInfoService;

    @Autowired
    private AliOSSUpload aliOSSUpload;

    /**
     * 查询试卷信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<BYKTSimulationInfoVo> list(BYKTSimulationInfoBo bo, PageQuery pageQuery) {
        return simulationInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出试卷信息列表
     */
    @Log(title = "试卷信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BYKTSimulationInfoBo bo, HttpServletResponse response) {
        List<BYKTSimulationInfoVo> list = simulationInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "试卷信息", BYKTSimulationInfoVo.class, response);
    }

    /**
     * 获取试卷信息详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BYKTSimulationInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(simulationInfoService.queryById(id));
    }

    /**
     * 新增试卷信息
     */
    @Log(title = "试卷信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@RequestBody BYKTSimulationInfoBo bo) throws Exception {
        return toAjax(simulationInfoService.insertByBo(bo));
    }

    /**
     * 修改试卷信息
     */
    @Log(title = "试卷信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BYKTSimulationInfoBo bo) throws Exception {
        return toAjax(simulationInfoService.updateByBo(bo));
    }

    /**
     * 删除试卷信息
     *
     * @param ids 主键串
     */
    @Log(title = "试卷信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(simulationInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
